<template>
  <div class="wrapper">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>会员管理</el-breadcrumb-item>
      <el-breadcrumb-item>会员日设置</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="easy-content"
      ref="wrap">
      <el-form :inline="true">
        <el-form-item>
          <el-date-picker v-model="startEndTime"
            style="height:34px;width:250px"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
            size="small"
            style="width:60px"
            @click="search">
            <i class="el-icon-search"></i>
          </el-button>
        </el-form-item>
        <el-form-item class="fr">
          <el-button type="primary"
            size="small"
            @click="_add" v-has="'memberconfigday'">
            新增会员日
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-table style="width: 100%"
            border
            max-height="550px"
            :data="days">
            <el-table-column prop="userTime"
              label="会员日">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="text"
                  @click="_delete(scope.row.id)" v-has="'memberconfigdaydelete'">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item class="fr">
          <el-pagination @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, prev , pager , next, sizes"
            :total="total">
          </el-pagination>
        </el-form-item>
      </el-form>
      <el-dialog title="新增会员日"
        :visible.sync="dialogVisible"
        @close="handleClose"
        width="800px">
        <el-form label-width="100px"
          :model="addData"
          :rules="rules"
          ref="form">
          <el-form-item label="批量增添类型">
            <el-radio-group v-model="addData.type"
              @change="toggleRadio">
              <el-radio :label="1">指定日期</el-radio>
              <el-radio :label="2">按周添加</el-radio>
              <el-radio :label="3">按月添加</el-radio>
            </el-radio-group>
          </el-form-item>
          <div v-if="addData.type==1">
            <el-form-item label="会员日"
              prop="date">
              <el-date-picker v-model="addData.date"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </div>
          <div v-if="addData.type==2">
            <el-form-item label="会员日"
              prop="weeks">
              <el-checkbox :indeterminate="isIndeterminate"
                v-model="checkAll"
                @change="handleCheckAllChange">全选</el-checkbox>
              <el-checkbox-group v-model="addData.weeks"
                @change="handleCheckedCitiesChange">
                <el-checkbox v-for="week in weeks"
                  :label="week.value"
                  :key="week.value">{{week.label}}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="生成日期范围"
              prop="rangeDates">
              <el-date-picker v-model="addData.rangeDates"
                style="height:34px;width:250px"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </div>
          <div v-if="addData.type==3">
            <el-form-item label="会员日"
              prop="date">
              <span>每月</span>
              <el-select v-model="addData.date"
                placeholder="请选择日期">
                <el-option v-for="(item,index) in dates"
                  :key="index"
                  :label="item"
                  :value="item"> </el-option>
              </el-select>
              <span>号</span>
            </el-form-item>
            <el-form-item label="生成日期范围"
              prop="rangeDates">
              <span>
                <el-date-picker v-model="addData.rangeDates[0]"
                  type="month"
                  value-format="yyyy-MM"
                  placeholder="选择开始月分">
                </el-date-picker> -
              </span>
              <span>
                <el-date-picker v-model="addData.rangeDates[1]"
                  type="month"
                  value-format="yyyy-MM"
                  placeholder="选择结束月分">
                </el-date-picker>
              </span>

            </el-form-item>
          </div>
        </el-form>
        <span slot="footer"
          class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
            @click="srue('form')">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getMemberDayList,
  deleteMemberDay,
  addMemberDay
} from "@/public/js/member";
export default {
  components: {},
  props: {},
  data() {
    var validate = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请选择生成日期范围"));
      } else if (value[0] >= this.addData.rangeDates[1]) {
        callback(new Error("请选择正确的开始日期和结束日期"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      startEndTime: [],
      checkAll: false,
      isIndeterminate: true,
      pageSize: 10,
      pageNo: 1,
      total: 0,
      dates: [],
      format: "yyy-MM-dd",
      weeks: [
        { label: "周一", value: 1 },
        { label: "周二", value: 2 },
        { label: "周三", value: 3 },
        { label: "周四", value: 4 },
        { label: "周五", value: 5 },
        { label: "周六", value: 6 },
        { label: "周天", value: 7 }
      ],
      days: [],
      addData: {
        type: 1,
        date: "",
        weeks: [],
        rangeDates: []
      },
      rules: {
        date: [
          {
            required: "true",
            message: "请选择会员日",
            trigger: ["change", "blur"]
          }
        ],
        rangeDates: [
          {
            validator: validate,
            required: "true",
            trigger: ["change", "blur"]
          }
        ],
        weeks: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个星期",
            trigger: "change"
          }
        ]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    //获取会员日列表数表
    _getMemberDayList() {
      getMemberDayList({
        startTime: this.startEndTime ? this.startEndTime[0] : "",
        endTime: this.startEndTime ? this.startEndTime[1] : "",
        pageSize: this.pageSize,
        pageNo: this.pageNo
      }).then(res => {
        this.days = res.data.days;
        this.total = res.data.total;
      });
    },
    //时间搜索
    search() {
      this.pageSize = 10;
      this.pageNo = 1;
      this._getMemberDayList();
    },
    //删除
    _delete(id) {
      console.log(id);
      this.$confirm("是否继续该删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteMemberDay({ id: id }).then(res => {
          if (!res.data.errCode) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.pageSize = 10;
            this.pageNo = 1;
            this._getMemberDayList();
          }
        });
      });
    },
    _rest() {
      this.$refs["form"].resetFields();
    },
    toggleRadio(e) {
      this._rest();
      this.addData.rangeDates = [];
      this.addData.weeks = [];
      this.addData.date = "";
    },
    //新增
    _add() {
      this.addData.type = 1;
      this.dialogVisible = true;
    },
    //关闭tanchuang
    handleClose() {
      this.$refs["form"].resetFields();
    },
    //baocun
    srue(formName) {
      console.log(this.addData);
      this.$refs[formName].validate(valid => {
        if (valid) {
          addMemberDay({
            type: this.addData.type,
            startTime: this.addData.rangeDates.length
              ? this.addData.rangeDates[0]
              : "",
            endTime: this.addData.rangeDates.length
              ? this.addData.rangeDates[1]
              : "",
            week: this.addData.weeks,
            day: this.addData.date
          }).then(res => {
            if (!res.data.errCode) {
              this.$message({
                type: "success",
                message: "新增成功"
              });
              this.dialogVisible = false;
              this._getMemberDayList();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleCheckAllChange(val) {
      const weeks = [];
      this.weeks.forEach(item => {
        weeks.push(item.value);
      });
      this.addData.weeks = val ? weeks : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      console.log(this.addData.weeks);
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.weeks.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.weeks.length;
    },
    //生成每月日期
    renderDays() {
      for (let i = 1; i <= 28; i++) {
        if (i < 10) {
          this.dates.push("0" + i);
        } else {
          this.dates.push(i);
        }
      }
    },
    //翻页
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this._getMemberDayList();
    },
    handleCurrentChange(pageNo) {
      this.pageNo = pageNo;
      this._getMemberDayList();
    }
  },

  created() {
    this.renderDays();
    this._getMemberDayList();
  },
  mounted() {}
};
</script>
<style lang="scss" scoped>
.wrapper {
  .el-range-editor.el-input__inner {
    padding: 0 10px;
  }
}
</style>